package com.jianan.mis.app;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.activities.WorkbenchActivityHelper;
import org.eclipse.ui.views.IViewDescriptor;

import com.jianan.mis.app.menu.MenuContent;
import com.jianan.mis.app.menu.MenuView;
import com.jianan.mis.data.domain.module.FunctionEntry;
import com.jianan.mis.data.domain.module.ITreeEntry;

public class AppPerspective implements IPerspectiveFactory {

	public static final String PERSPECTIVE_ID = "com.jianan.mis.app.AppPerspective";

	private IFolderLayout mainLayout;

	public void createInitialLayout(IPageLayout layout) {
		layout.setEditorAreaVisible(false);

		layout.addStandaloneView(MenuView.ID, true, IPageLayout.LEFT, 0.20f,
				layout.getEditorArea());
		mainLayout = layout.createFolder("right", SWT.RIGHT, 0.80f, layout
				.getEditorArea());
		
		addPlaceHolderForModules();
	}

	private void addPlaceHolderForModules() {
		List<ITreeEntry> modules = MenuContent.getMenu();
		for (ITreeEntry module : modules) {
			for (ITreeEntry function : module.getChildren()) {
				FunctionEntry functionEntry = (FunctionEntry) function;
				IViewDescriptor viewDescriptor = PlatformUI.getWorkbench()
						.getViewRegistry().find(functionEntry.getViewId());
				if (!WorkbenchActivityHelper.filterItem(viewDescriptor)) {
					mainLayout.addPlaceholder(functionEntry.getViewId());
				}
			}
		}
	}

}
